import { request } from '@/utils/request'
import type {
  LoginPwdRequest,
  UserInfo,
  CaptchaResponse,
  CaptchaVerifyRequest,
  LoginResponse,
} from '@/types/auth'
import type { BaseResponse } from '@/types/response'
import { LoginType } from '@/config'

export const authService = {
  /**
   * 密码登录
   * @param params 登录参数
   * @param loginType 登录类型
   */
  loginByPassword: (params: LoginPwdRequest, loginType: LoginType = LoginType.ADMIN) =>
    request.post<BaseResponse<UserInfo | LoginResponse>>(`/saLogin/${loginType}/doPwd`, {
      body: params,
      skipErrorHandler: true,
      returnBaseResponse: true,
    }),

  /**
   * 获取当前登录用户信息
   * @param loginType 登录类型
   */
  getCurrentUser: (loginType: LoginType) => request.get<UserInfo>(`/saLogin/${loginType}/current`),

  /**
   * 生成验证码
   */
  generateCaptcha: () => request.get<CaptchaResponse>('/captcha/generateCaptcha'),

  /**
   * 验证验证码
   */
  verifyCaptcha: (params: CaptchaVerifyRequest) =>
    request.post<string>('/captcha/verifyCaptcha', {
      body: params,
    }),

  /**
   * 退出登录
   * @param loginType 登录类型
   */
  async logout(loginType: LoginType): Promise<BaseResponse<string>> {
    return request.get(`/saLogin/${loginType}/doLogout`, {
      skipErrorHandler: true, // 跳过默认错误处理
    })
  },
}
